using System.Configuration;
using System.Web.Services;
using System.Xml;
using System;

namespace umlungu.engineX.workflowX {
	/// <summary>
	/// umlungu workflow web services
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class workflowX : x_result {
		#region Private properties
		private x_workflow workflowx;
		private x_workitem workitemx;
		private x_workitems workitemsx;
		private x_workprocess workprocessx;
		private x_workstatus workstatusx;
		private x_workstate workstatex;
		private x_workreport workreportx;
		private string dsn;
		#endregion

		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		public workflowX() : base("workflowX") {
			dsn = System.Configuration.ConfigurationManager.AppSettings["WorkspaceDSN"];
		}
		#endregion

		#region Work-Item web service methods
		/// <summary>
		/// Create a work-item (for a user)
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		/// <param name="AgentID">The Agent's identifier</param>
		/// <param name="UserID">The User's identifier</param>
		/// <param name="UserName">The User's full name</param>
		/// <param name="UserContact">The User's contact number</param>
		/// <param name="UserEmail">The User's email address</param>
		/// <param name="Request">The request title / description</param>
		/// <param name="RequestXml">An xml formatted string of the request data</param>
		[WebMethod] public XmlDocument CreateWorkItem(int ProcessID, int AgentID, int UserID, string UserName, string UserContact, string UserEmail, string Request, string RequestXml) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Create(ProcessID, AgentID, UserID, UserName, UserContact, UserEmail, Request, RequestXml);
					workitemx.Get();
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get work-item (for a user)
		/// </summary>
		/// <param name="WorkItemID">The work-item's identifier</param>
		[WebMethod] public XmlDocument GetWorkItem(int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Get(WorkItemID);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get a list of work-items (for a user)
		/// </summary>
		/// <param name="UserID">The User's identifier</param>
		[WebMethod] public XmlDocument GetWorkItemList(int UserID) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workitemsx.Get(UserID);
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region Admin Interface web service methods
		/// <summary>
		/// Get a list of tasks (for an agent)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="AgentID">The Agent's identifier</param>
		[WebMethod] public XmlDocument GetTasks(string Token, string AgentID) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetTaskList");
					workitemsx.GetWorkItems(AgentID);
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get current task (for an agent)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		[WebMethod] public XmlDocument GetTask(string Token, int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetTask");
					workitemx.GetCurrent(WorkItemID);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Complete task (for an agent)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="Comment">A free-text comment</param>
		/// <param name="PersonID">The identifier of a person (user, agent or owner)</param>
		[WebMethod] public XmlDocument CompleteTask(string Token, int WorkItemID, int PersonID, string Comment) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "CompleteTask");
					workitemx.Complete(WorkItemID, Comment, PersonID);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Re-assign task (for an agent)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="AgentID">The Agent's identifier</param>
		/// <param name="Comment">A free-text comment</param>
		[WebMethod] public XmlDocument ReassignTask(string Token, int WorkItemID, int AgentID, string Comment) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "ReassignTask");
					workitemx.Get(WorkItemID);
					workitemx.AgentID = AgentID;
					workitemx.Update(Comment);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region Admin Interface web service methods
		/// <summary>
		/// Get a list of all tasks in the system
		/// </summary>
		/// <param name="Token">The authentication token</param>
		[WebMethod] public XmlDocument GetAllWork(string Token) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetAllWork");
					workitemsx.GetWorkItems();
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get a list of all tasks for an administrator
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="OwnerID">The process Owner's identifier</param>
		[WebMethod] public XmlDocument GetAllWorkItems(string Token, string OwnerID) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetAllTasks");
					workitemsx.GetWorkItemsByOwner(OwnerID);
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get a list of selected tasks for an administrator
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="OwnerID">The process Owner's identifier</param>
		/// <param name="StateID">Selected tasks' work-state identifier, '0' if all</param>
		/// <param name="StatusID">Selected tasks' work-status identifier, '0' if all</param>
		/// <param name="AgentID">Selected tasks' agent identifier, '0' if all</param>
		[WebMethod] public XmlDocument GetSelectWorkItems(string Token, string OwnerID, int StateID, int StatusID, int AgentID) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetAllTasks");
					workitemsx.GetWorkItemsByOwner(OwnerID, StateID, StatusID, AgentID);
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get a list of tasks by process (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="ProcessID">The work-process identifier</param>
		[WebMethod] public XmlDocument GetWorkItemsByProcess(string Token, int ProcessID) {
			using (workitemsx = new x_workitems(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetTasksByProcess");
					workitemsx.GetWorkItemsByProcess(ProcessID);
					_AddOk(workitemsx.WorkItems.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get a list of processes (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		[WebMethod] public XmlDocument GetProcesses(string Token) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "ReassignProcess");	// only get if can re-assign?
					workprocessx.GetProcess();	// can also be GetProcesses for a more complete list
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Re-assign process (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="ProcessID">The work-process identifier</param>
		[WebMethod] public XmlDocument ReassignProcess(string Token, int WorkItemID, int ProcessID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "ReassignProcess");
					workitemx.ID = WorkItemID;
					workitemx.ProcessID = ProcessID;
					//workitemx.Get();
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Re-assign work-item (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="AgentID">The Agent's identifier</param>
		/// <param name="Comment">A free-text comment</param>
		[WebMethod] public XmlDocument ReassignWorkItem(string Token, int WorkItemID, int AgentID, string Comment) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "ReassignWorkItem");
					workitemx.Get(WorkItemID);
					workitemx.AgentID = AgentID;
					workitemx.Update(Comment);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Delete work-item (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		[WebMethod] public XmlDocument DeleteWorkItem(string Token, int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "DeleteWorkItem");
					workitemx.Delete(WorkItemID);
					_AddOk();
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Update work-item (for an administrator)
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="StateID">The work-state identifier</param>
		/// <param name="StatusID">The work-status identifier</param>
		[WebMethod] public XmlDocument UpdateWorkItem(string Token, int WorkItemID, int StateID, int StatusID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "UpdateWorkItem");
					workitemx.Update(WorkItemID, StateID, StatusID);
					workitemx.Get();
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region Notification web service methods
		/// <summary>
		/// Notify user of task status
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="Message">The text of the message</param>
		/// <param name="MessageType">The message type, ie 'email' or 'sms'</param>
		[WebMethod] public XmlDocument NotifyUser(string Token, int WorkItemID, string Message, string MessageType) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "NotifyUser");
					workitemx.Notify(WorkItemID, Message, true);
					_AddOk();
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Notify agent/administrator of task status
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="WorkItemID">The work-item's identifier</param>
		/// <param name="Message">The text of the message</param>
		/// <param name="MessageType">The message type, ie 'email' or 'sms'</param>
		[WebMethod] public XmlDocument NotifyAgent(string Token, int WorkItemID, string Message, string MessageType) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "NotifyAgent");
					workitemx.Notify(WorkItemID, Message, false);
					_AddOk();
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region System services: Work-Process web service methods
		/// <summary>
		/// Gets a list of all the work-statuses in the system
		/// </summary>
		[WebMethod] public XmlDocument GetWorkProcesses() {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					_AddOk(workprocessx.Processes.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Create a work-process
		/// </summary>
		/// <param name="Name">A short, identifying name of the process</param>
		/// <param name="Description">A description of the process</param>
		/// <param name="Duration">The desired maximum duration of the process</param>
		/// <param name="Xml">The process defintion xml</param>
		/// <param name="OwnerID">The process Owner's identifier</param>
		[WebMethod] public XmlDocument CreateWorkProcess(string Name, string Description, int Duration, string Xml, int OwnerID) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workprocessx.Create(Name, Description, Duration, Xml, OwnerID);
					workprocessx.Get();
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Gets the work-process
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		[WebMethod] public XmlDocument GetWorkProcess(int ProcessID) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workprocessx.Get(ProcessID);
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Saves and updates an existing work-process
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		/// <param name="Name">A short, identifying name of the process</param>
		/// <param name="Description">A description of the process</param>
		/// <param name="Duration">The desired maximum duration of the process</param>
		/// <param name="Xml">The process defintion xml</param>
		/// <param name="OwnerID">The process Owner's identifier</param>
		[WebMethod] public XmlDocument SaveWorkProcess(int ProcessID, int ProcessTypeID, string Name, string Description, int Duration, string Xml, int OwnerID) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
                    workprocessx.Save(ProcessID, Name, Description, Duration, Xml, OwnerID, ProcessTypeID);
					workprocessx.Get();
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Gets the agents for a work-process
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		[WebMethod] public XmlDocument GetAgents(int ProcessID) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workprocessx.GetAgents(ProcessID);
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Adds an agent to the list of agents for a work-process
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		/// <param name="AgentID">The Agent's identifier</param>
		/// <param name="Firstname">The Agent's first name</param>
		/// <param name="Surname">The Agent's surname</param>
		/// <param name="Email">The Agent's email</param>
		/// <param name="Cellphone">The Agent's cell number</param>
		[WebMethod] public XmlDocument AddAgent(int ProcessID, int AgentID, string Firstname, string Surname, string Email, string Cellphone) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workprocessx.AddAgent(ProcessID, AgentID, Firstname, Surname, Email, Cellphone);
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Deletes an agent from the list of agents for a work-process
		/// </summary>
		/// <param name="ProcessID">The process identifier</param>
		/// <param name="AgentID">The Agent's identifier</param>
		[WebMethod] public XmlDocument DeleteAgent(int ProcessID, int AgentID) {
			using (workprocessx = new x_workprocess(dsn)) {
				try {
					workprocessx.DeleteAgent(ProcessID, AgentID);
					_AddOk(workprocessx.WorkProcess.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region System services: Work-Status web service methods
		/// <summary>
		/// Gets a list of all the work-statuses in the system
		/// </summary>
		[WebMethod] public XmlDocument GetWorkStatuses() {
			using (workstatusx = new x_workstatus(dsn)) {
				try {
					_AddOk(workstatusx.Statuses.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Create a work-status
		/// </summary>
		/// <param name="Name">A short, identifying name of the status</param>
		/// <param name="Description">A description of the status</param>
		[WebMethod] public XmlDocument CreateWorkStatus(string Name, string Description) {
			using (workstatusx = new x_workstatus(dsn)) {
				try {
					workstatusx.Create(Name, Description);
					workstatusx.Get();
					_AddOk(workstatusx.WorkStatus.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Gets the work-status
		/// </summary>
		/// <param name="StatusID">The status identifier</param>
		[WebMethod] public XmlDocument GetWorkStatus(int StatusID) {
			using (workstatusx = new x_workstatus(dsn)) {
				try {
					workstatusx.Get(StatusID);
					_AddOk(workstatusx.WorkStatus.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Saves and updates an existing work-status
		/// </summary>
		/// <param name="StatusID">The status identifier</param>
		/// <param name="Name">A short, identifying name of the status</param>
		/// <param name="Description">A description of the status</param>
		[WebMethod] public XmlDocument SaveWorkStatus(int StatusID, string Name, string Description) {
			using (workstatusx = new x_workstatus(dsn)) {
				try {
					workstatusx.Save(StatusID, Name, Description);
					workstatusx.Get();
					_AddOk(workstatusx.WorkStatus.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region System services: Work-State web service methods
		/// <summary>
		/// Gets a list of all the work-states in the system
		/// </summary>
		[WebMethod] public XmlDocument GetWorkStates() {
			using (workstatex = new x_workstate(dsn)) {
				try {
					_AddOk(workstatex.States.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Create a work-state
		/// </summary>
		/// <param name="Name">A short, identifying name of the state</param>
		/// <param name="Description">A description of the state</param>
		[WebMethod] public XmlDocument CreateWorkState(string Name, string Description) {
			using (workstatex = new x_workstate(dsn)) {
				try {
					workstatex.Create(Name, Description);
					workstatex.Get();
					_AddOk(workstatex.WorkState.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Gets the work-state
		/// </summary>
		/// <param name="StateID">The state identifier</param>
		[WebMethod] public XmlDocument GetWorkState(int StateID) {
			using (workstatex = new x_workstate(dsn)) {
				try {
					workstatex.Get(StateID);
					_AddOk(workstatex.WorkState.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Saves and updates an existing work-state
		/// </summary>
		/// <param name="StateID">The state identifier</param>
		/// <param name="Name">A short, identifying name of the state</param>
		/// <param name="Description">A description of the state</param>
		[WebMethod] public XmlDocument SaveWorkState(int StateID, string Name, string Description) {
			using (workstatex = new x_workstate(dsn)) {
				try {
					workstatex.Save(StateID, Name, Description);
					workstatex.Get();
					_AddOk(workstatex.WorkState.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region System services: Work-Report web service methods
		/// <summary>
		/// Gets a report of all the work in the system
		/// </summary>
		/// <param name="Token">The authentication token</param>
		/// <param name="OwnerID">The process Owner's identifier, '0' if all</param>
		/// <param name="StatusID">Reported work work-status identifier, '0' if all</param>
		/// <param name="Datetype">Reported work period, 0,1 or 2 for year, week or month</param>
		/// <param name="Dateval">Number of work periods from current date</param>
		[WebMethod] public XmlDocument GetReport(string Token, int OwnerID, int StatusID, int Datetype, int Dateval) {
			using (workreportx = new x_workreport(dsn)) {
				try {
					workflowx = new x_workflow();
					workflowx.Validate(Token, "GetAllTasks");	// as for GetSelectWorkItems
					workreportx.GetWorkReport(OwnerID, StatusID, Datetype, Dateval);
					_AddOk(workreportx.Report.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion

		#region Temporary services: testing purposes
		/// <summary>
		/// Login to the workflow system (via gatekeeper)
		/// </summary>
		/// <param name="Username">A gatekeeper username</param>
		/// <param name="Password">A gatekeeper password</param>
		[WebMethod] public XmlDocument Login(string Username, string Password) {
			using (workflowx = new x_workflow()) {
				try {
					workflowx.Login(Username, Password);
					_AddOk(workflowx.User.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Gets an agent's details (via gatekeeper)
		/// </summary>
		/// <param name="UserID">A gatekeeper identifier</param>
		[WebMethod] public XmlDocument GetAgent(int UserID) {
			using (workflowx = new x_workflow()) {
				try {
					workflowx.GetUser(UserID);
					_AddOk(workflowx.User.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Starts the workflow for a work-item
		/// </summary>
		/// <param name="WorkItemID">The work-item identifier</param>
		[WebMethod] public XmlDocument StartWorkFlow(int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Start(WorkItemID);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Finishes the workflow for a work-item
		/// </summary>
		/// <param name="WorkItemID">The work-item identifier</param>
		[WebMethod] public XmlDocument FinishWorkFlow(int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Finish(WorkItemID);
					_AddOk(workitemx.WorkItem.OuterXml);
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get the state of a work-item
		/// </summary>
		/// <param name="WorkItemID">The work-item identifier</param>
		[WebMethod] public XmlDocument GetState(int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Get(WorkItemID);
					_AddOk("State", workitemx.StateID.ToString());
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		/// <summary>
		/// Get the status of a work-item
		/// </summary>
		/// <param name="WorkItemID">The work-item identifier</param>
		[WebMethod] public XmlDocument GetStatus(int WorkItemID) {
			using (workitemx = new x_workitem(dsn)) {
				try {
					workitemx.Get(WorkItemID);
					_AddOk("Status", workitemx.StatusID.ToString());
				} 
				catch(x_exception e)		{_AddError(e);}
				catch(System.Exception e) 	{_AddError(e);}
			}
			return(Result);
		}
		#endregion
	}
}
